<template>
  <el-breadcrumb class="app-breadcrumb" :separator-icon="ArrowRight">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
        <span v-if="item.redirect === 'noRedirect' || index == levelList.length - 1" class="no-redirect">{{ item.meta.title }}</span>
        <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script setup>
import { ArrowRight } from '@element-plus/icons-vue'
import usePermissionStore from '@/store/modules/permission'
// import { constantRoutesTest } from '@/router'
const route = useRoute();
const router = useRouter();
const levelList = ref([])
// const routList = ref(JSON.parse(JSON.stringify(constantRoutesTest))) //12级菜单
const permissionStore = usePermissionStore()
const routList=ref(permissionStore.routesAll)
function getBreadcrumb() {
  // only show routes with meta.title
  let matched = route.matched.filter(item => item.meta && item.meta.title);
  const first = matched[0]
  let companyRole = localStorage.getItem('companyRole') || routList.value[0].companyRole
  let tagName = localStorage.getItem('tagName') || routList.value[0].children[0].companyRole
  let intemObj=[]
  routList.value.forEach(item => {
        if(item.companyRole==companyRole){
          item.children.forEach(intemInfo=>{
            if(intemInfo.companyRole==tagName){
              intemObj.push(intemInfo)
            }
          })
        }
      
    });
  // 判断是否为首页
  if (!isDashboard(first)) {
    // let routList.value
        if(intemObj.length>0){
          matched =intemObj.concat(matched)

        }else{
          matched = [{ path: '/index', meta: { title: '首页' } }].concat(matched)

        }
  }else if(matched.length==1){
    if(intemObj.length>0){
      matched =intemObj.concat(matched)
    }
  }

  levelList.value = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
}
function isDashboard(route) {
  const name = route && route.name
  if (!name) {
    return false
  }
  return name.trim() === 'Index'
}
function handleLink(item) {
  // return false
  const { redirect, path } = item
  console.log('redirect',redirect)
  console.log('path',path)
console.log('item',item)
  if (redirect) {
    router.push(redirect)
    return
  }

  if(path=='/index'){
    let companyRole = localStorage.getItem('companyRole') //1级菜单标识
    let tagName = localStorage.getItem('tagName') //2级菜单标识

    // let tips=companyRole+'-'+tagName
    let tips=tagName
    router.push({ path: '/index', query: { comRole: tips } })
  }else{
    if(item.children){
     let pathList=[]
       item.children.forEach((itemInfo) => {
        if(itemInfo.type==1 && itemInfo.hidden==0){
          pathList.push(itemInfo.name)
        }
      })
      if(pathList.length>0){
        router.push({ name: pathList[0]})
      }
    }else{
      router.push({ path: path,})
    }
  }
}

watchEffect(() => {
  // if you go to the redirect page, do not update the breadcrumbs
  if (route.path.startsWith('/redirect/')) {
    return
  }
  getBreadcrumb()
})
getBreadcrumb();
</script>

<style lang='scss' scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 20px;

  .no-redirect {
    color: #2158F3;
    cursor: text;
  }
}
</style>